import json

users = [
    {
        'username': 'admin',
        'password': '123'
    },
    {
        'username': 'jack',
        'password': '123'
    }]

db_file = 'orders.json'


def read_json():
    try:
        with open(db_file, 'r') as f:
            orders_json = json.load(f)
        return orders_json
    except Exception as e:
        print('读取失败：', e)
        return {}


def write_json(orders_json):
    try:
        with open(db_file, 'w') as f:
            json.dump(orders_json, f, indent=4)
        print('写入成功')
    except Exception as e:
        print('写入失败：', e)


def login():
    """
    登录，登陆成功返回用户名
    :return:
    """
    name = input('请输入用户名：')
    password = input('请输入密码：')
    for user in users:
        if user['username'] == name and user['password'] == password:
            return name
        elif user['username'] == name and user['password'] != password:
            print('密码错误')
            return None
    print('用户不存在')
    return None


def auth(func):
    """
    装饰器，登录用户为管理员是才允许进行操作
    :param func:
    :return:
    """

    def check_admin():
        name = login()

        if name == 'admin':
            func()  # 获取订单
        elif name is None:
            return
        else:
            print('权限不足')

    return check_admin


def get_order(order_id):
    order_id = str(order_id)
    if order_id in read_json():
        print(f'查询到订单{order_id}：{read_json()[order_id]}')
    else:
        print(f'您查询的订单{order_id}不存在')


@auth
def get_orders():
    print(read_json())


def add_order():
    order_id, product_name, product_price, product_count = input_order_info()

    with open(db_file, 'r', encoding='utf-8') as f:
        orders_json = json.load(f)

    if order_id in orders_json:
        print('订单已存在')
        return False
    else:
        orders_json[order_id] = {
            'product_name': product_name,
            'product_price': product_price,
            'product_count': product_count
        }

        write_json(orders_json)

        return True


def update_order():
    order_id, product_name, product_price, product_count = input_order_info()
    with open(db_file, 'r', encoding='utf-8') as f:
        orders_json = json.load(f)
    if order_id in orders_json:
        orders_json[order_id] = {
            'product_name': product_name,
            'product_price': product_price,
            'product_count': product_count
        }
        write_json(orders_json)

        return True
    else:
        print('订单不存在')
        return False


def input_order_info():
    order_id = input('请输入订单ID：')
    if not order_id.isdigit():
        print('订单ID必须是数字')
        return
    product_name = input('请输入商品名称：')
    product_price = input('请输入商品价格：')
    product_count = input('请输入商品数量：')
    return order_id, product_name, product_price, product_count


def run():
    while True:
        print('1.查询订单')
        print('2.查询所有订单')
        print('3.添加订单')
        print('4.修改订单')
        print('5.退出')
        choice = input('请选择操作：')
        if choice == '1':
            order_id = input('请输入订单ID：')
            get_order(order_id)
        elif choice == '2':
            get_orders()
        elif choice == '3':
            add_order()
        elif choice == '4':
            update_order()
        elif choice == '5':
            break
        else:
            print('无效的操作')


if __name__ == '__main__':
    run()
